
#include <iostream>
#include <unistd.h>
#include <memory>

#include"TcpServer.hpp"
#include"HttpProtocol.hpp"


using namespace std;
using namespace Network;

//网络负责IO发送
//HandlerHttpRequest字节流数据解析和调用业务处理方法
//处理报文的方法
//我们认为我们已经读到了一个完整请求
string HandlerHttpRequest(string &request)
{

    HttpRequest req;


    //反序列化
    req.Deserialize(request);
    req.Debug();

    cout << "Received request: " << request << endl;
    string content="<html><h1>hello bite</h1></html>";
    string httpstatusline = "HTTP/1.1 200 OK\r\n";
    string httpheader="Content-Length: "+to_string(content.length())+"\r\n";
    httpheader+="\r\n";

    string httpresponse = httpstatusline + httpheader + content;
    return httpresponse;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "Usage:\n     ./udp_echo_server <port>" << endl;
        return 0;
    }

    uint16_t localport = stoi(argv[1]);
    
    unique_ptr<TcpServer> svr(new TcpServer(localport, HandlerHttpRequest));
    svr->Loop();
    return 0;
}
